 var CsdnScriptPlugin999 = {
    /// <summary>
    /// 接口版本
    /// </summary>
    interfaceVersion: "1.0",

    /// <summary>
    /// 插件标题，显示给用户看
    /// </summary>
    caption: "由兔子党Tim(q107770540)提供的个人签名插件+兔子表情插件",

    /// <summary>
    /// 设计者在CSDN的ID 
    /// </summary>
    designer: "muzizongheng",

    /// <summary>
    /// 按钮对象，可选项
    /// </summary>
    buttons: {},

    /// <summary>
    /// 分隔条对象，可选项
    /// </summary>
    separators: {},

    /// <summary>
    /// 装载
    /// </summary>
    load: function() {
    this.separators["Signature"] = CsdnScriptWorkshop.addSeparator();
    this.buttons["Signature"] = CsdnScriptWorkshop.addButton(
            this.caption,
            "/PointForum/ui/scripts/csdn/Plugin/999/vertical.gif",
            function() {
                var text = CsdnScriptWorkshop.getEditorText();
                 
                text += "\n\n\n[color=#C0C0C0]====================================论坛签名==================================[/color]\n"
                text += getUserInfor();
                text += "[color=#FF0000]\n当您的问题得到解答后请及时结贴.[/color]\n";
                text += "[url=http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html][/url]\n";
                text += "[url=http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html][/url]\n";
                text += "[url=http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html][/url]\n\n";
                text += "[color=#FF0000]如何给分和结贴？[/color]\n[url=http://community.csdn.net/Help/HelpCenter.htm#结帖][/url]\n\n";

                text += "[color=#FF0000]访问我的blog[/color]\n[url=http://blog.csdn.net/muzizongheng][/url]\n\n";
                text += "[url=http://bbtg.sdo.com/1180342035][img=http://bambook.sdo.com/images/AdBonus/img10.jpg][/img][/url]";
                CsdnScriptWorkshop.setEditorText(text);
            
            });
        this.separators["icon"] = CsdnScriptWorkshop.addSeparator(); // 添加一个分隔条

        this.buttons["icon"] = CsdnScriptWorkshop.addButton( // 添加一个工具按钮
            "由兔子党Tim(q107770540)提供的兔子表情插件",
            "http://tim-tang.googlecode.com/files/24.gif",
            function() {
                var htmlDialog = str_Html();
                var point = absolutePoint(this); // 按钮的位置

                CsdnScriptWorkshop.showDialog("兔子表情", htmlDialog, point.x, point.y + 18, 400, 220);

            }); 
    },

    /// <summary>
    /// 卸载
    /// </summary>
    free: function() {
        for (var button in this.buttons)
            CsdnScriptWorkshop.deleteButton(this.buttons[button]);
        for (var separator in this.separators)
            CsdnScriptWorkshop.deleteSeparator(this.separators[separator]);
    }
}
var jsn; //全局的json
var imgCount =60;
var userName = ""; //同一浏览器可以存放不同用户的Cookie
function str_Html() {
    //获得用户名
    userName = window.parent.document.getElementById("vName").innerHTML == '游客' ? 'youke' : window.parent.document.getElementById("vName").innerHTML;
//    jsn = GetMyCookie();
    var str = "<div style=\" width:360px; height:200px; position:relative\"><div id=\"div2\" style=\"padding:10px;width:335px; height:200px;\">";
    for (var i = 1; i <= imgCount; i++) {
        str += "<img src=\"http://tim-tang.googlecode.com/files/" + i + ".gif\" title=\"" + i + "\" width=\"20px\" height=\"20px\" style=\"margin:2px; border:1px solid Green;\" onmouseover=\"doit(this)\" />";
    }
    
    str += "<div style=\"text-align:right; vertical-align:bottom; padding-right:5px;position:relative;padding-bottom:20px;padding-bottom:20px\">";
    if (getCookie("sortFlag") == "1")
        str += "<a href=\"#\" id=\"sortFlag\" onclick=\"setSort(this);return false;\" style=\"font-size:11px;\" title=\"ON\"></a> ";
    else
        str += "<a href=\"#\" id=\"sortFlag\" onclick=\"setSort(this);return false;\" style=\"font-size:11px;\" title=\"OFF\"></a> ";

    str += "<a href=\"#\" onclick=\"setmycookie();return false;\" style=\"font-size:11px;\"></a> <a href=\"#\" onclick=\"getmycookie();return false;\" style=\"font-size:11px;\"></a> <a href=\"#\" onclick=\"delmycookie();return false;\" style=\"font-size:11px;\"></a> <a href=\"#\" onclick=\"aboutme();return false;\" style=\"font-size:11px;\"></a></div>";

    str += "</div>";
    str += " <div id=\"showimg\" style=\" background-color:White; border-color:Green; border-style:solid; border-width:1px; width: 40px; height: 40px; display: none; position: absolute;z-index: 9999; text-align:center; vertical-align:middle\" onmouseover=\"this.style.cursor='hand'\" onmouseout=\"closeDiv()\" onclick=\"doreturn()\"><img id=\"img\" style=\"width: 32px; height: 32px; position:relative; top:3px\" /></div>";
    str += "</div>"
    return str;
}



//获得存储的Cookie返回Json
function GetMyCookie() {
    var str = getCookie(userName + 'cookie');

    //假如没有cookie就重新生成json格式的数据返回，并且存储Cookie
    if (str == '' || str == null || str == 'undefind') {
        returnstr = [{ 'n': '暴牙', 'c': '0' }, { 'n': '鄙视', 'c': '0' }, { 'n': '打', 'c': '0' }, { 'n': '打人', 'c': '0' }, { 'n': '大哭', 'c': '0' }, { 'n': '大笑', 'c': '0' }, { 'n': '得意', 'c': '0' }, { 'n': '调皮', 'c': '0' }, { 'n': '发呆', 'c': '0' }, { 'n': '尴尬', 'c': '0' }, { 'n': '鼓掌', 'c': '0' }, { 'n': '哈喽凯蒂', 'c': '0' }, { 'n': '害羞', 'c': '0' }, { 'n': '寒', 'c': '0' }, { 'n': '汗', 'c': '0' }, { 'n': '惊讶', 'c': '0' }, { 'n': '可爱', 'c': '0' }, { 'n': '酷', 'c': '0' }, { 'n': '困', 'c': '0' }, { 'n': '流泪', 'c': '0' }, { 'n': '媒婆', 'c': '0' }, { 'n': '牛', 'c': '0' }, { 'n': '喷火', 'c': '0' }, { 'n': '喷血', 'c': '0' }, { 'n': '飘游', 'c': '0' }, { 'n': '亲吻', 'c': '0' }, { 'n': '求', 'c': '0' }, { 'n': '色', 'c': '0' }, { 'n': '闪', 'c': '0' }, { 'n': '扇人', 'c': '0' }, { 'n': '伤心', 'c': '0' }, { 'n': '生气', 'c': '0' }, { 'n': '胜利', 'c': '0' }, { 'n': '睡', 'c': '0' }, { 'n': '叹气', 'c': '0' }, { 'n': '挑逗', 'c': '0' }, { 'n': '投降', 'c': '0' }, { 'n': '吐白沫', 'c': '0' }, { 'n': '微笑', 'c': '0' }, { 'n': '问', 'c': '0' }, { 'n': '吓', 'c': '0' }, { 'n': '鲜花', 'c': '0' }, { 'n': '小胡子', 'c': '0' }, { 'n': '心', 'c': '0' }, { 'n': '阴险的笑', 'c': '0' }, { 'n': '晕', 'c': '0' }, { 'n': '再见', 'c': '0' }, { 'n': '炸', 'c': '0' }, { 'n': '抓', 'c': '0' }, { 'n': '抓狂', 'c': '0'}];
        var jsnstr = json2str(returnstr);

        //Cookie
        setCookie(userName + 'cookie', jsnstr, 365);
        return returnstr;
    }
    else {
        return str2json(str);
    }
}


//点击表情的时候执行方法
function doreturn() {
    if (document.getElementById('sortFlag').title == 'ON') {
        //更新表情点击次数
        var jsn1 = UpdateCount(document.getElementById("img").getAttribute("title"), jsn);
        //对表情（按照点击率）排序
        jsn = json2str(sortIt(jsn1));
        //将新的排序存储到Cookie
        setCookie(userName + 'cookie', jsn, 365);
    }
    //向编辑器中写入图片
    setText("[img=" + document.getElementById("img").src + "][/img]");
}

//更新表情的点击数
//返回新的json
function UpdateCount(name, jsn1) {
    for (var i = 0; i < jsn1.length; i++) {
        if (jsn1[i].n == name) {
            jsn1[i].c = parseInt(jsn[i].c) + 1;
            return jsn1;
        }
    }
}

//排序表情的顺序（按照count倒序排列）
//方法来源：http://topic.csdn.net/u/20090212/16/AFE83EB3-DFC4-46D0-81BC-ACD5BF8168B4.html
function sortIt(jsn1) {
    return jsn1.sort(function (a, b) { return parseInt(a["c"], 10) > parseInt(b["c"], 10) ? -1 : parseInt(a["c"], 10) == parseInt(b["c"], 10) ? 0 : 1 });
}


// json对象转字符串形式
function json2str(o) {
    var str = "";
    for (var i = 0; i < o.length; i++) {
        str += o[i].n;
        str += ',' + o[i].c + "|";
    }

    return str.substring(0, str.lastIndexOf('|'));
}

//针对上面的字符串格式转成json
function str2json(str) {
    var jsn;
    var s = "[";
    var arr = str.split('|');
    for (var i = 0; i < arr.length; i++) {
        var ar = arr[i].split(',');
        s += "{'n':'" + ar[0] + "','c':'" + ar[1] + "'},";
    }
    s = s.substring(0, s.lastIndexOf(',')) + "]";
    return eval('(' + s + ')');
}

//控制div显示的表情的大图片
function doit(obj) {
    obj.style.cursor = "hand";
    document.getElementById("img").src = obj.src;
    document.getElementById("img").setAttribute("title", obj.getAttribute("title"));
    var div = document.getElementById("showimg");
    div.style.display = "";
    var div_h = div.style.height.replace('px', '');
    var div_w = div.style.width.replace('px', '');
    div.style.top = obj.offsetTop + 'px';
    div.style.left = obj.offsetLeft + 'px';
}
//关闭DIV
function closeDiv() {
    var div = document.getElementById("showimg");
    div.style.display = "none";
}
//向编辑器中写表情的路径
function setText(str) {
    CsdnScriptWorkshop.setSelectText(str);
    CsdnScriptWorkshop.closeDialog();
}

//获得存储表情顺序的字符串对象
function getmycookie() {
    window.prompt('请复制下面的内容,方便下次使用↓', json2str(jsn));
}
//自定义表情顺序
function setmycookie() {
    var str = window.prompt('请粘贴内容↓', "");
    var jsn1;
    try {
        if (str == null) return; //点击取消
        jsn1 = str2json(str);
        if (jsn1.length == 50) {
            jsn = sortIt(jsn1);
            setCookie(userName + 'cookie', json2str(jsn), 365);
        }
        else alert("导入失败！");
    } catch (e) { alert("导入失败！") }
}
//重新初始化表情
function delmycookie() {
    setCookie(userName + 'cookie', '', 365);
}

//一些信息
function aboutme() {
     
}

//设置是否自动排序
function setSort(obj) {
    if (obj.title == 'OFF') {
        setCookie('sortFlag', '1', 30);
        obj.title = "ON";
        obj.innerHTML = "关闭自动排序";
    }
    else {
        setCookie('sortFlag', '0', 30);
        obj.title = "OFF";
        obj.innerHTML = "打开自动排序";
    }
}
function getUserInfor() {
    if (window.ActiveXObject) {
        var txt = window.parent.document.getElementById("bbscsdn_wrap").innerHTML;
        var reg = /<LI title=总技术分：\d+；总技术排名：\d+><DFN><A onclick=LogClickCount\(this,\d+\)[\s\S]*<\/LABEL> /g;
        var str = txt.match(reg);

        var reg1 = /总技术分：\d+/g;
        var reg2 = /总技术排名：\d+/g;
        var reg3 = /http\:\/\/hi.csdn.net\/\w+/g;
        var reg4 = /<LABEL[\s\S]*LABEL>/g;

        var zjsf = str[0].match(reg1)[0].replace("总技术分：", "");
        var zjspm = str[0].match(reg2)[0].replace("总技术排名：", "");
        var mingzi = str[0].match(reg3)[0].replace("http://hi.csdn.net/", "");

        var txt2 = str[0].match(reg4)[0];
        var reg5 = /\'[\s\S]*\'/g;
        var txt3 = txt2.match(reg5)[0];

        var reg6 = /截至[\s\S]*\\n用户结帖率/g;
        var reg7 = /总发帖：\d+/g;
        var reg8 = /正常结帖：\d+/g;
        var reg9 = /未结帖：\d+/g;
        var reg10 = /用户结帖率[\s\S]*\\n总发帖/g;

        var sj = txt3.match(reg6)[0].replace("截至", "").replace("\\n用户结帖率", "");
        var zft = txt3.match(reg7)[0].replace("总发帖：", "");
        var zcjt = txt3.match(reg8)[0].replace("正常结帖：", "");
        var wjt = txt3.match(reg9)[0].replace("未结帖：", "");
        var jtl = txt3.match(reg10)[0].replace("用户结帖率", "").replace("\\n总发帖", "");

        var edi_txt = "\n\r" + mingzi + ":你好!\n";
        edi_txt += "截至 " + sj + " 前:\n你已发帖 " + zft + " 个,\t\t\t未结贴 [color=#FF0000]" + wjt + "[/color] 个;\n结贴率为： [color=#FF0000]" + jtl + "[/color]\n";
        return edi_txt;
    }
    else {
        return "";
    }

}